package com.topfinance.cnaps2.web.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.topfinance.frame.util.StringUtil;
import com.topfinance.frame.web.BaseSupport;
import com.topfinance.payment.ebo.TCfgTransInEbo;
import com.topfinance.payment.service.ITCfgTransInMgr;
import com.opensymphony.xwork2.ModelDriven;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author
 * @version 1.50
 *          <p>
 *          Copyright: Copyright (c) 2004 2005
 *          </p>
 *          <p>
 *          Company:
 *          </p>
 */
public final class CfgTransinAction extends BaseSupport implements ModelDriven<TCfgTransInEbo> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1923903369408173973L;

	private ITCfgTransInMgr itcfgtransoutmgr = null;

	private TCfgTransInEbo transoutEbo = new TCfgTransInEbo();

	public TCfgTransInEbo getModel() {
		return transoutEbo;
	}

	public void setCfgTransInMgr(ITCfgTransInMgr itcfgtransoutmgr) {
		this.itcfgtransoutmgr = itcfgtransoutmgr;
	}

	public CfgTransinAction() {
		super();
	}

	public String execute() {
		return list();
	}
	
	public String create() { 
		try {
			log.debug(StringUtil.mapObject(transoutEbo));
			itcfgtransoutmgr.addTransin(transoutEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "" + transoutEbo.getOid();
			rtnResultMap.put("info", getText(CREATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String update() {
		try {
			log.debug(StringUtil.mapObject(transoutEbo));
			itcfgtransoutmgr.editTransin(transoutEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(UPDATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String delete() {
		try {
			String delId = request.getParameter("id");
			itcfgtransoutmgr.deleteTransin(new Integer(delId));
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(DELETE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	

	public String list() {
		log.debug("CfgTransAction-list begin");
		try {
			List<TCfgTransInEbo> l = itcfgtransoutmgr.getAllTransIn();
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("CfgTransAction-list end");
		return SUCCESS;
	}

	public TCfgTransInEbo getTCfgTransInEbo() {
		return transoutEbo;
	}

	public void setTCfgTransInEbo(TCfgTransInEbo transoutEbo) {
		this.transoutEbo = transoutEbo;
	}

	public String query() {
		if (request.getParameter("id") == null) {
			rtnResultMap = new HashMap();
			String[] args = new String[1];
			args[0] = "";// you can add extra message here
			rtnResultMap.put("info", getText("expect.valid.pk.data", args));
			return SUCCESS;
		}
		try {
			String uid = request.getParameter("id");
			TCfgTransInEbo trans = itcfgtransoutmgr.getTransin(new Integer(uid));
			List l = new ArrayList<TCfgTransInEbo>();
			l.add(trans);
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, 1);
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	

	public String test() {
//		log.debug("ConfRoutetion-test begin");
//		try {
//			ConfTransportEbo tEbo = new ConfTransportEbo();
//			tEbo.setName("测试传输");
//			tEbo.setPrefix("PREFIX_PP_AMQ");
//			tEbo.setProvider("Test");
//			tEbo.setClassName("a.n.c");
//			tEbo.setBrokerUrl("tcp://dfd");
//			transportManager.addTrans(tEbo);
//			rtnResultMap = new HashMap();
//			rtnResultMap.put("info", getText("create.success", "Test Success!"));
//			return SUCCESS;
//		} catch (Exception e) {
//			handleException(e);
//		}
		log.debug("ConfRoutetion-list end");
		return SUCCESS;
	}

}
